function MakeLegends(type, dirName, format, resolution, fillfactor)

% make the legends for the map and cell maps
% modified pie chart function

if nargin < 3
    format = '-djpeg';
end
if nargin < 4
    resolution = [5 5];
end
if nargin < 5
    fillfactor = 0;
end


x = 200; % resolution of colours = no. of segments
h = figure;
set(h, 'PaperUnits','centimeters');
set(h, 'Position', [0 0 600 600]);
set(h, 'PaperPosition', [0 0 resolution]);

maxpts = 1000;    % resolution of single segments

cax = axescheck(x);
cax = newplot(cax);
next = lower(get(cax, 'NextPlot'));
hold_state = ishold(cax);

switch type
    case 'dir'
        theta0 = -pi / 4;  % starting point
        hue_table = MakeHueTable(x);
    case 'ori'
        theta0 = pi / 4;  % starting point
        hue_table = repmat(MakeHueTable(x/2), 2, 1);
end


legend = [];
n = max(1, ceil(maxpts * 1/x));
for i = 1:x
    r = [0; ones(n + 1, 1); 0];
    r2 = [0; repmat(fillfactor, n + 1, 1); 0];
    theta = theta0 - [0; 1/x * (0:n)' / n; 0] * 2 * pi; % the - sign makes the pie chart get filled clockwise
    [xx,yy] = pol2cart(theta, r);
    [xx2,yy2] = pol2cart(theta, r2);
    xx = [xx(2:n+2);xx2(n+2:-1:2)]; yy = [yy(2:n+2);yy2(n+2:-1:2)];
    theta0 = min(theta); % the "outer" bound is the minimum, since we are going clockwise
    legend = [legend, patch('XData', xx, 'YData', yy, 'CData', i * ones(size(xx)), ...
        'FaceColor', hue_table(i,:), 'LineStyle', 'none', 'parent', cax)];
end

if ~hold_state,
    view(cax, 2); set(cax, 'NextPlot', next);
    axis(cax, 'equal', 'off')
end

if ~isempty(dirName)
    switch type
        case 'dir'
            print(format, fullfile(dirName, 'legend_dir'));
        case 'ori'
            print(format, fullfile(dirName, 'legend_ori'));
    end
end
% close;